<?php use_helper('jQuery'); ?>
<?php include_javascripts(); ?>
<?php if($sf_user->isAuthenticated())  $uid = $sf_user->getGuardUser()->getId() ?>

<script type="text/javascript">
	function addcomment(comid)
{	
	var newcom = document.createElement('div');
	newcom.className = 'bodycomment';
	newcom.id = 'bodycomment'+comid;
	
	var h2 = document.createElement('h2');
	h2.className = 'h2';
	h2.innerHTML = document.getElementById("h2").innerHTML;

	var cbody = document.createElement('div');
	cbody.id = 'lastcomment';
	cbody.className = 'cbody';

	var date = document.createElement('em');
	date.className = 'date';
	var now = Date();
	date.innerHTML = 'только что' ; //now.toLocaleString();

	var del = document.createElement('em');
	del.className = 'date';
	del.innerHTML = '<a onclick="jQuery.ajax({type:\'POST\',dataType:\'html\',complete:function(XMLHttpRequest, textStatus){$(&quot;#bodycomment' + comid + '&quot;).hide();},url:\'<?php echo url_for('comment/delete') ?>/comid/'+ comid +'\'}); return false;" href="#">удалить</a>';
	
	newcom.appendChild(h2);
	newcom.appendChild(cbody);
	newcom.appendChild(date);
	newcom.appendChild(del);	

	var bdc = document.getElementById("bodycomment");
	bdc.parentNode.appendChild(newcom);	
	
}
</script>

<hr style='height: 1px; color: #EEE'>

<div id="bigcommentblock">

<?php
	if (count($comments)>0)
	{
		foreach ($comments as $comment) {
			?>

<div class="bodycomment" id="bodycomment<?php echo $comment['id']; ?>">
<div class='h2'><?php include_component('myaccount','showuserblock',array('userid'=>$comment['SfGuardUser']['id'])) //$comment['SfGuardUser']['username'];?>
</div>
<div class="cbody"><?php echo $comment['body']; ?></div>
<em class="date"><?php echo format_date(strtotime($comment['created_at']),'D','ru');?></em>
			<?php if(isset($uid)) { if ( $comment['user_id'] == $uid) { ?> <em
	id="delete" class="date"><?php echo jq_link_to_remote("удалить", array('url' 		=> 'comment/delete?comid='.$comment['id'],
																		   'complete' 	=> '$("#bodycomment'.$comment['id'].'").hide();' ), array()) ;?></em>
	<?php }
	elseif($allowdeleteanothercomment) { ?> <em id="delete" class="date"><?php echo jq_link_to_remote("удалить", array('url' 		=> 'comment/deletefromindex?comid='.$comment['id'].'&photoid='.$entity_id,
																		   'complete' 	=> '$("#bodycomment'.$comment['id'].'").hide();' ), array()) ;?></em>
	<?php }} ;
	?></div>

	<?php };
	if ($sf_user->isAuthenticated()) { ?> <!--  EMPTY DEFAULT COMMENT BLOCK FOR CURRENT USER  -->

<div id="bodycomment" class="bodycomment" style="display: none;">
<div id="h2" class='h2'><?php include_component('myaccount','showuserblock',array('userid'=>$sf_user->getGuardUser()->getId())) //$comment['SfGuardUser']['username'];?>
</div>
<div id="cbody" class="cbody"></div>
<em id="date" class="date"><?php echo format_date(time(),'D','ru');?></em>
<em id="delete" class="date"><?php echo "удалить";?></em></div>

	</div>    <!-- CLOSE #bigcomment BLOCK -->
	<?php } ?>
	<?php } else	{
		if ($sf_user->isAuthenticated()) {

			//TODO: Сократить следующий блок!
			?>
<!--  EMPTY DEFAULT COMMENT BLOCK FOR CURRENT USER  -->

<div id="bodycomment" class="bodycomment" style="display: none;">
<div id="h2" class='h2'><?php include_component('myaccount','showuserblock',array('userid'=>$sf_user->getGuardUser()->getId())) //$comment['SfGuardUser']['username'];?>
</div>
<div id="cbody" class="cbody"></div>
<em id="date" class="date"><?php echo format_date(time(),'D','ru');?></em>
<em id="delete" class="date"><?php echo "удалить";?></em></div>
</div>
		<?php } ?>
<div id='noquestions'><i>На данный момент отзывов еще нет</i> <br />
<br />
</div> <!-- CLOSE #bigcomment BLOCK -->


		<?php }
		if ($sf_user->isAuthenticated()){
			?>
<div id="update" style="display: none">сохранение</div>

<?php echo jq_form_remote_tag(	array('url' 		=> 'comment/submit',
												  'datatype'	=> 'text',
												  'loading' 	=> '$("#update").show();',									  												 
											  	  'complete'	=> '$("#update").hide(); addcomment(XMLHttpRequest.responseText); $("#lastcomment").text( $("#body").attr("value") ); $("#lastcomment").removeAttr("id"); $("#body").attr("value","");  $("#noquestions").hide();'
											  	  ),
											  	  array('style'=>'display:block',
												  'autocomplete'=>'off') ); ?>

											  	  <?php echo $form; ?>

<input type='hidden'
	name='<?php echo 'entity_id' ?>' value='<?php echo $entity_id ?>' />
<input
	type='hidden' name='<?php echo 'entity_type' ?>'
	value='<?php echo $entity_type ?>' />
<br />
<input
	type='submit' class='commentbutton' value='Оставить отзыв' />
</form>

											  	  <?php
		}
		else
		{
			?>
</div>
<i>Войди, чтобы оставлять отзывы</i>
<?php } ?>